﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Forest.Lion.Data;
using Forest.Lion.Model;
using System.Collections.ObjectModel;
using Forest.Lion.Domain.Models;
using Forest.Lion.Domain.Contracts;
using Forest.Lion.Domain.Shared;
using Forest.Lion.Infrastructure;

namespace Forest.Lion.Domain.Services
{
    public class CandidateAppService : ICandidateAppService
    {
        private readonly ICandidateRepository _candidateRepository;
        private readonly ILovRepository _lovRepository;
        private readonly IResumeRepository _resumeRepository;
        private readonly IJobPositionRepository _jobPositionRepository;
        private readonly ITagRepository _tagRespository;

        public CandidateAppService(ICandidateRepository candidateRepository,
            ILovRepository lovRepository, IResumeRepository resumeRepository,
            IJobPositionRepository jobPositionRepository, ITagRepository tagRepository)
        {
            _candidateRepository = candidateRepository;
            _lovRepository = lovRepository;
            _resumeRepository = resumeRepository;
            _jobPositionRepository = jobPositionRepository;
            _tagRespository = tagRepository;
        }

        public IEnumerable<CandidateModel> GetAppliedCandidates(Guid companyId)
        {
            //var appliedCandidates = _candidateRepository
            //   .GetAll()
            //   .Where(c => c.Deleted == false && c.Source == 1)
            //   .OrderByDescending(c => c.DateSaved)
            //   .ToList();
            return null;
        }

        public IPagedList<CandidateDomainModel> SearchCandidates(Guid companyId, string keyWord,
            CandidateQueryStatus queryStatus, int pageIndex, int pageSize)
        {
            var list = _candidateRepository.GetAll().Where(s => s.CompanyId == companyId);
            if (queryStatus != CandidateQueryStatus.All)
            {
                int status = (int)queryStatus;
                list = list.Where(s => s.Status == status);
            }
            var searchList = from item in list
                             join jobItem in _jobPositionRepository.All()
                             on item.JobPositionId equals jobItem.Id
                             join resumeItem in _resumeRepository.All()
                             on item.ResumeId equals resumeItem.Id
                             join tagItem in _tagRespository.All()
                             on new { item.TagId.HasValue, item.TagId.Value }
                                equals new {HasValue = true,Value = tagItem.Id}
                                into tagList
                                from tag in tagList.DefaultIfEmpty()
                             where jobItem.JobTitle.Contains(keyWord)
                             orderby item.LastUpdated
                             select new CandidateDomainModel()
                             {
                                 Id = item.Id,
                                 BirthDate = resumeItem.BirthDate,
                                 Gender = resumeItem.Gender,
                                 SaveDate = item.DateCreated,
                                 Source = item.Source,
                                 ResumeId = resumeItem.Id,
                                 RefreshTime = resumeItem.LastUpdated,
                                 IsReadLetter = item.IsReadLetter,
                                 EducationLevel = resumeItem.EducationLevel,
                                 TagId = item.TagId,
                                 TagName = tag != null ? tag.TagName : "",
                                 FullName = resumeItem.FullName,
                                 JobPositionId = jobItem.Id,
                                 Status = item.Status,
                                 JobTitle = jobItem.JobTitle,
                                 Phone  = (resumeItem.Mobile != null && resumeItem.Mobile != "") ?
                                        resumeItem.Mobile :　resumeItem.Tel,
                                        YearsOfExperience = resumeItem.YearsOfExperience,
                                        
                             };

            return new PagedList<CandidateDomainModel>(searchList, pageIndex, pageSize);
        }
    }
}
